package m.jclecai.com.pay;

import android.content.Intent;
import android.net.Uri;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.PayChannelBean;
import m.jclecai.com.utils.ChannelUtil;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by Dinosa on 2017/12/29.
 */

public class WebPayPayImpl extends BasePayImpl {

    public String mUserName;
    public String mPassword;
    public String mPayMoney;

    public WebPayPayImpl(BaseActivity activity, String userName, String password) {
        super(activity);
        mUserName = userName;
        mPassword = password;
    }


    @Override
    public void pay(PayChannelBean channelBean, String payMoney) {
        setPaying(false);
        mPayMoney = payMoney;
        getWebTradNo(channelBean);
    }

    /**
     * web的订单方式；
     */
    private void getWebTradNo(PayChannelBean payChannelBean) {

        showDialog();
        String userName = mUserName;
        String password = mPassword;

        HttpManager.getAsyn("charge.php?channel=" + payChannelBean.getChannel() + "&password=" + password + "&user=" + userName + "&money=" + mPayMoney + "&appstore=" + ChannelUtil.getChannel(), new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
            }

            @Override
            public void onResponse(String response) {

                JsonObject obj = new Gson().fromJson(response, JsonObject.class);
                if (obj != null) {
                    //{"message":"订单号生成成功","money":"20","projectno":"C2018031017450075967","status":"0","ipaddr":"220.249.92.34"}
                    JsonElement projectno = obj.get("projectno");
                    if (projectno == null) {
                        ToastUtil.showToast(mActivity, obj.get("message").getAsString());
                        return;
                    }
                    //保存订单号；
                    setOrderNumber(projectno.getAsString());
                    //拿到顶订单号
                    getPayRequest(projectno.getAsString());
                } else {
                    dissDialog();
                }
            }
        });
    }

    public void getPayRequest(String mPayMoney, String out_trade_no) {
        showDialog();
        this.mPayMoney = mPayMoney;
        getPayRequest(out_trade_no);
    }

    private void getPayRequest(String out_trade_no) {
        //这里调用我们自己的接口；
        Map<String, String> params = new HashMap<>();
        params.put("body", "大彩神支付");
        params.put("out_trade_no", out_trade_no);
        params.put("total_fee", mPayMoney);
        params.put("body", "[android]pay");
        //payment/yspay/request.php
        HttpManager.postAsyn("payment/scpayh5/request.php", new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay("充值失败");
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                //使用浏览器打包；
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("https://mapi.alipay.com/gateway.do" + parserURLParams(response));
                intent.setData(content_url);
                mActivity.startActivity(intent);
                //这里是设置支付状态；
                setPaying(true);
            }
        }, params);
    }


}
